#include <myheader_c_plus.h>
using std::cout;
using std::endl;
using std::istream;
using std::ostream;
using std::string;
using std::cin;
class String 
{
public:
    //默认构造函数
    String()
        :_pstr(nullptr){

        }


    //构造函数
    String(const char * str)
        :_pstr(new char[strlen(str) + 1]())
    {
        strcpy(_pstr,str);
    }

    //拷贝构造函数
    String(const String & rhs)
        :_pstr(new char[strlen(rhs._pstr) + 1]())
    {
        strcpy(_pstr, rhs._pstr);
    }

    //析构函数
    ~String(){
        delete [] _pstr;
        _pstr = nullptr;
    }

    //赋值运算符
    String &operator=(const String & rhs){
        if(this != &rhs){
            delete [] _pstr;
            _pstr = new char[strlen(rhs._pstr) + 1]();
            strcpy(_pstr, rhs._pstr);
        }
        return *this;
    }

    //运算符重载
    String &operator=(const char * str){
        if(_pstr != nullptr){
            delete [] _pstr;
            _pstr = new char[strlen(str) + 1]();
            strcpy(_pstr,str);
        }
        return *this;
    }

    //+=运算符重载
    String &operator+=(const String & rhs){
        strcat(_pstr, rhs._pstr);
        return *this;
    }

    //+=运算符重载函数重载
    String &operator+=(const char *str){
        strcat(_pstr, str);
        return *this;
    }

    //[]运算符重载
    char &operator[](std::size_t index){
        size_t size = strlen(_pstr);
        //cout << size << endl;
        if(index > size - 1){
            cout << "out of range" << endl;
            static char ch = '\0';
            return ch;
        }
        return _pstr[index];
    }


    const char &operator[](std::size_t index) const{
        size_t size = strlen(_pstr);
        cout << "in const" << endl;
        //cout << size << endl;
        if(index > size - 1){
            cout << "out of range" << endl;
            static char ch = '\0';
            return ch;
        }
        return _pstr[index];

    }

    std::size_t size() const{
        size_t size = strlen(_pstr);
        return size;
    }
    const char* c_str() const{
        return _pstr;
    }

    friend bool operator==(const String &lhs, const String &rhs);
    friend bool operator!=(const String &lhs, const String &rhs);

    friend bool operator<(const String &, const String &);
    friend bool operator>(const String &, const String &);
    friend bool operator<=(const String &, const String &);
    friend bool operator>=(const String &, const String &);

    friend std::ostream &operator<<(std::ostream &os, const String &s);
    friend std::istream &operator>>(std::istream &is, String &s);
    void print(){
        cout << _pstr << endl;
    }
private:
    char * _pstr;
};
bool operator==(const String & lhs, const String & rhs){
    if(strcmp(lhs._pstr, rhs._pstr) ==  0){
        return true;
    } 
    return false;
}
bool operator!=(const String & lhs, const String & rhs){
    if(strcmp(lhs._pstr, rhs._pstr) !=  0){
        return true;
    }
    return false;
}                                             

bool operator<(const String &lhs, const String &rhs){
    if(strcmp(lhs._pstr, rhs._pstr) < 0){
        return true;
    }
    return false;
}
bool operator>(const String &lhs, const String &rhs){
    if(strcmp(lhs._pstr, rhs._pstr) > 0){
        return true;
    }
    return false;
}
bool operator<=(const String &lhs, const String &rhs){
    if(strcmp(lhs._pstr, rhs._pstr) < 0 || strcmp(lhs._pstr, rhs._pstr) == 0){
        return true;
    }
    return false;
}
bool operator>=(const String &lhs, const String &rhs){

    if(strcmp(lhs._pstr, rhs._pstr) > 0 || strcmp(lhs._pstr, rhs._pstr) == 0){
        return true;
    }
    return false;
}

ostream &operator<<(std::ostream &os, const String &s){
    if(s._pstr != nullptr){
        os << s._pstr;
    }
    return os;
}
istream &operator>>(std::istream &is, String &s){
    if(s._pstr !=nullptr){
        string str;
        is  >> str;
        delete [] s._pstr;
        s._pstr = new char[str.length() + 1];
        strcpy(s._pstr, str.c_str());
    }
    return is;
}
String operator+(const String &lhs, const String &rhs);
String operator+(const String &, const char *);
String operator+(const char *, const String &);
int main(int argc, char *argv[])
{
    String str1("hello");
    str1.print();
    String str2 = str1;
    str2.print();
    String str3;
    str3 = str2;
    str3.print();
    str1 = "world";
    str1.print();
    str2 += str1;
    str2.print();
    str2+="lwj";
    str2.print();
    cout << "===================" << endl;
    cout << str2[0] << endl;
    const String str4("nihao");
    cout << str4[2] << endl;
    cout << "size :" << str4.size() << endl;
    cout << "c_str() :" << str4.c_str() << endl;
    String str5("nhao");
    if(str5 != str4){
        cout << "not equal" << endl;
    }else{
        cout << "equal" << endl;
    }

    cout << "==========================" << endl;
    String s1("aniha");
    String s2("bniha");
    String s3("bniha");
    if(s2 >= s3 ){
        cout << "s1 >= s2" << endl;
    }else{
        cout << "s1 < s2" << endl;
    }

    cout << s1 << endl;
    cin >> s2 ;

    cout << s2 << endl;
    return 0;
}

